﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using App_Code;

public partial class Stats_Statistics : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(Request.QueryString["keyword"]) && !IsPostBack)
        {
            return;
        }
        if (IsPostBack)
        {
            SetStats();
            return;
        }
        Keywords.SelectedValue = Request.QueryString["keyword"];
    }
    protected void Keywords_DataBound(object sender, EventArgs e)
    {
        
        if (Keywords.Items.Count == 0)
        {
            Keywords.Items.Insert(0, new ListItem("No keywords", String.Empty));
        }
        SetStats();
    }

    private void SetStats()
    {
        var context = Context.GetEntityContext();
        var keyword = (from k in context.Keywords where k.Keyword == Keywords.SelectedValue select k).FirstOrDefault();
        if (keyword == null)
        {
            return;
        }
        TotalLabel.Text += keyword.Tweets.Count;
        DateTime today = DateTime.Today;
        var todayTweets = (from t in keyword.Tweets where t.Date.Date == today select t).ToList();
        TodayLabel.Text += todayTweets.Count;
        var thisMonthTweets =
            (from t in keyword.Tweets where t.Date.Year == today.Year && t.Date.Month == today.Month select t).ToList();
        ThisMonthLabel.Text += thisMonthTweets.Count;
        var lastWeekTweets = (from t in keyword.Tweets
                              where t.Date.DayOfYear > (today.DayOfYear - 7) select t).ToList();
        LastWeekLabel.Text += lastWeekTweets.Count;
    }
}